/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.tabbedproperties.composite;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.BehavioralFeature;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;

import com.cea.papyrus.core.commands.factories.GenericCommandFactory;
import com.cea.papyrus.core.editor.MultiDiagramEditor;
import com.cea.papyrus.core.tabbedproperties.composite.ISectionComposite;

/**
 * 
 */
public class IsAbstractComposite implements ISectionComposite {

	/**
	 * 
	 */
	private Element element;

	/**
	 * 
	 */
	protected DirectEditSelectionListener directEditSelectionListener = new DirectEditSelectionListener();
	
	/**
	 * 
	 * 
	 * @return the element
	 */
	public Element getElement() {
		return element;
	}

	/**
	 * 
	 * 
	 * @param element the element to set
	 */
	public void setElement(Element element) {
		this.element = element;
	}

	/**
	 * 
	 */
	private Button isAbstract = null;

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
	 */
	/**
	 * 
	 * 
	 * @param factory 
	 * @param parent 
	 * 
	 * @return 
	 */
	public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
		Composite composite = factory.createFlatFormComposite(parent);
		FormData data;

		isAbstract = factory.createButton(composite, "", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 85);
		data.top = new FormAttachment(0, 0);
		isAbstract.setLayoutData(data);
		isAbstract.addSelectionListener(directEditSelectionListener);

		CLabel abstractionLabel = factory.createCLabel(composite, "Abstract:"); //$NON-NLS-1$
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(isAbstract,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(isAbstract, 0, SWT.CENTER);
		abstractionLabel.setLayoutData(data);

		return composite;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.composites.ISectionComposite#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(! isAbstract.isDisposed()){
			isAbstract.removeSelectionListener(directEditSelectionListener);

			if(element instanceof Classifier){
				isAbstract.setSelection(((Classifier)getElement()).isAbstract());
			} else if(element instanceof BehavioralFeature) {
				isAbstract.setSelection(((BehavioralFeature)getElement()).isAbstract());
			}
			isAbstract.addSelectionListener(directEditSelectionListener);
		}
	}
	
	/**
	 * Disposing listeners.
	 */
	public void dispose(){
		if(isAbstract!=null && !isAbstract.isDisposed())
			isAbstract.removeSelectionListener(directEditSelectionListener);
	}
	
	/**
	 * Listener for a direct edit manager.
	 * 
	 * @author Remi SCHNEKENBURGER
	 */
	protected class DirectEditSelectionListener implements org.eclipse.swt.events.SelectionListener {
		
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			if(element instanceof ElementImport){// in the case of element import
				// do nothing
			} else if((element instanceof Classifier) || (element instanceof BehavioralFeature)) {
				Command command = GenericCommandFactory.eINSTANCE.createCommand(element, "IsAbstract", new Object[] {isAbstract.getSelection()});
				//Darpan
				IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
				CommandStack stack = (CommandStack)editor.getAdapter(CommandStack.class);
				stack.execute(command);
			}
		}

		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e){
		}
	};
}
